using Digitalmes.Domain.Entities.Mes.Masters;

namespace Digitalmes.Application.Mes.Masters.Materials.Queries;

/// <summary>
/// 物料查询命令。
/// </summary>
public sealed record GetMaterialListQuery(MaterialTypeEnum? Type) : IRequest<IResult<List<MdMaterialDto>>>;

internal sealed class GetMaterialListQueryHandler(IRepository<MdMaterial> repo) : IRequestHandler<GetMaterialListQuery, IResult<List<MdMaterialDto>>>
{
    public async Task<IResult<List<MdMaterialDto>>> Handle(GetMaterialListQuery request, CancellationToken cancellationToken)
    {
        var items = await repo.AsQueryable()
            .WhereIF(request.Type.HasValue, m => m.Type == request.Type)
            .ToListAsync(cancellationToken);
        return Result.From(items.Adapt<List<MdMaterialDto>>());
    }
}
